﻿<template>
  <a-modal
    title="编辑科室信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="DEPT_CODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入DEPT_CODE " v-decorator="['deptCode']" />
        </a-form-item>
        <a-form-item label="DEPT_NAME " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入DEPT_NAME " v-decorator="['deptName']" />
        </a-form-item>
        <a-form-item label="DEPT_ATTR1 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入DEPT_ATTR1 " v-decorator="['deptAttr1']" />
        </a-form-item>
        <a-form-item label="DEPT_ATTR2 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入DEPT_ATTR2 " v-decorator="['deptAttr2']" />
        </a-form-item>
        <a-form-item label="INPUT_CODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入INPUT_CODE " v-decorator="['inputCode']" />
        </a-form-item>
        <a-form-item label="INPUT_CODE_WB " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入INPUT_CODE_WB " v-decorator="['inputCodeWb']" />
        </a-form-item>
        <a-form-item label="DEPT_ADDR " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入DEPT_ADDR " v-decorator="['deptAddr']" />
        </a-form-item>
        <a-form-item label="SICKBEDS " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入SICKBEDS " v-decorator="['sickbeds']" />
        </a-form-item>
        <a-form-item label="REMARK1 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK1 " v-decorator="['remark1']" />
        </a-form-item>
        <a-form-item label="REMARK2 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK2 " v-decorator="['remark2']" />
        </a-form-item>
        <a-form-item label="REMARK3 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK3 " v-decorator="['remark3']" />
        </a-form-item>
        <a-form-item label="REMARK4 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK4 " v-decorator="['remark4']" />
        </a-form-item>
        <a-form-item label="REMARK5 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK5 " v-decorator="['remark5']" />
        </a-form-item>
        <a-form-item label="REMARK6 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK6 " v-decorator="['remark6']" />
        </a-form-item>
        <a-form-item label="REMARK7 " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK7 " v-decorator="['remark7']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['iD']" /></a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择创建时间" v-decorator="['createDate']" @change="onChangecreateDate"/>
        </a-form-item>
        <a-form-item label="创建者Id" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入创建者Id" v-decorator="['createUserId']" />
        </a-form-item>
        <a-form-item label="创建者名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入创建者名称" v-decorator="['createUserName']" />
        </a-form-item>
        <a-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择更新时间" v-decorator="['modifyDate']" @change="onChangemodifyDate"/>
        </a-form-item>
        <a-form-item label="修改者Id" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入修改者Id" v-decorator="['modifyUserId']" />
        </a-form-item>
        <a-form-item label="修改者名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入修改者名称" v-decorator="['modifyUserName']" />
        </a-form-item>
        <a-form-item label="删除时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择删除时间" v-decorator="['deleteDate']" @change="onChangedeleteDate"/>
        </a-form-item>
        <a-form-item label="删除者ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入删除者ID" v-decorator="['deleteUserId']" />
        </a-form-item>
        <a-form-item label="删除者姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入删除者姓名" v-decorator="['deleteUserName']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    SysDepartmentEdit
  } from '@/api/modular/main/SysDepartmentManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        createDateDateString: '',
        modifyDateDateString: '',
        deleteDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              iD: record.iD,
              deptCode: record.deptCode,
              deptName: record.deptName,
              deptAttr1: record.deptAttr1,
              deptAttr2: record.deptAttr2,
              inputCode: record.inputCode,
              inputCodeWb: record.inputCodeWb,
              deptAddr: record.deptAddr,
              sickbeds: record.sickbeds,
              remark1: record.remark1,
              remark2: record.remark2,
              remark3: record.remark3,
              remark4: record.remark4,
              remark5: record.remark5,
              remark6: record.remark6,
              remark7: record.remark7,
              createUserId: record.createUserId,
              createUserName: record.createUserName,
              modifyUserId: record.modifyUserId,
              modifyUserName: record.modifyUserName,
              deleteUserId: record.deleteUserId,
              deleteUserName: record.deleteUserName
            }
          )
        }, 100)
        this.form.getFieldDecorator('createDate', { initialValue: moment(record.createDate, 'YYYY-MM-DD') })
        this.createDateDateString = moment(record.createDate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('modifyDate', { initialValue: moment(record.modifyDate, 'YYYY-MM-DD') })
        this.modifyDateDateString = moment(record.modifyDate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('deleteDate', { initialValue: moment(record.deleteDate, 'YYYY-MM-DD') })
        this.deleteDateDateString = moment(record.deleteDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.createDate = this.createDateDateString
            values.modifyDate = this.modifyDateDateString
            values.deleteDate = this.deleteDateDateString
            SysDepartmentEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangecreateDate(date, dateString) {
        this.createDateDateString = dateString
      },
      onChangemodifyDate(date, dateString) {
        this.modifyDateDateString = dateString
      },
      onChangedeleteDate(date, dateString) {
        this.deleteDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
